package com.xxpn.controller.server;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args){

        try {
            System.out.println("Socket服务器开始运行...");
            ServerSocket serverSocket = new ServerSocket(9999);
            while (true){
                Socket socket = serverSocket.accept();
                new Thread(new Server_listen(socket)).start();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}

class Server_listen implements Runnable{
    private Socket socket;

    Server_listen(Socket socket){
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

            while (true){
                if(ois!=null){


                    Object object=ois.readObject();

                    JSONObject json=JSON.parseObject(object.toString());
                    System.out.println(object);


                    String myid=json.get("myid").toString();
                    String type=json.get("type").toString();

                    switch(type){
                        case  "login" :

                            Socket mySocket=AllSocket.map.get(myid);
                            if(mySocket==null){
                                AllSocket.map.put(myid ,socket);
                            }
                            break;
                        case "talk":
                            String youid=json.get("youid").toString();
                            String huifu=json.get("msg").toString();
                            Socket yousocket= AllSocket.map.get(youid);
                            if(yousocket!=null){
                                new Thread(new Server_send(yousocket,huifu)).start();
                            }else{
                                Socket mysocket= AllSocket.map.get(myid);
                                String huifu2="你发送的id 不在线";
                                new Thread(new Server_send(mysocket,huifu2)).start();
                            }
                            break;
                        case "heart":
                            break;
                    }



                }


            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                socket.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

class Server_send implements Runnable{
    private Socket socket;

    private String msg;

    Server_send(Socket socket,String msg){
        this.socket = socket;
        this.msg = msg;
    }

    @Override
    public void run() {
        try {
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
           // while (true){
               System.out.println("请输入要发送的内容：");
                JSONObject object = new JSONObject();
                object.put("type","回复");
                object.put("msg",msg);
                 oos.writeObject(object);
                oos.flush();
           // }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}